package com.example.communist_app_api.controller.uniapp;
import com.example.communist_app_api.db.dto.MessageDTO;
import com.example.communist_app_api.service.MessageService;
import com.example.communist_app_api.service.WordService;
import com.example.communist_app_api.util.CosUtils;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.uniapp.req.UniAppCreatMessageReq;
import com.example.communist_app_api.vo.uniapp.req.UniAppCreatSonMessageReq;
import com.example.communist_app_api.vo.uniapp.req.UniAppDeleteMessageReq;
import com.example.communist_app_api.vo.uniapp.req.UniAppSelectByConditionReq;
import com.example.communist_app_api.vo.web.resp.SensitiveWordApiResp;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/3/1 22:21
 */
@RestController
@RequestMapping("/uniApp/message")
@Api("建言献策板块")
public class UniAppMessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private WordService wordService;

    public static final int auditor_id = 0;

    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public R createPhoto(@ApiParam("建言献策图片")
                         @NotBlank @RequestBody MultipartFile multipartFile){
        String photoUrl = CosUtils.uploadFile(multipartFile, "");
        return R.ok("上传成功").put("photoUrl",photoUrl);
    }

    @PostMapping("/create")
    @ApiOperation("新增留言")
    public R createMessage(@RequestBody UniAppCreatMessageReq req , HttpServletRequest httpServletRequest){
        Integer userId = getUserId(httpServletRequest);
        SensitiveWordApiResp resp = wordService.detectSensitiveWord(req.getContent(), String.valueOf(userId));
        Boolean isSensitiveWord = resp.getIsSensitiveWord();
        if (!isSensitiveWord && req.getPhotoUrlList() ==null) {
            //false时为没有敏感词出现，就新增,审核人就以系统审核即可
            messageService.createMessageBySystem(req.getContent(),userId,auditor_id);
        }else {
            messageService.createMessage(req.getContent(),req.getPhotoUrlList(),userId);
        }
        return R.ok("新增成功");
    }

    @PostMapping("/createSonMessage")
    @ApiOperation("新增子留言")
    public R createSonMessage(@RequestBody UniAppCreatSonMessageReq req , HttpServletRequest httpServletRequest){
        Integer userId = getUserId(httpServletRequest);
        SensitiveWordApiResp resp = wordService.detectSensitiveWord(req.getContent(), String.valueOf(userId));
        Boolean isSensitiveWord = resp.getIsSensitiveWord();
        if (!isSensitiveWord) {
            //true时为没有敏感词出现，就新增,审核人就以系统审核即可
            messageService.createSonMessageBySystem(req.getContent(),req.getMessageId(),userId,auditor_id);
        }else {
            messageService.createSonMessage(req.getContent(),req.getMessageId(),userId);
        }
        return R.ok("新增子留言成功");
    }

    @PostMapping("/selectMessage")
    @ApiOperation("查询留言")
    public R selectMessage(@RequestBody UniAppSelectByConditionReq req ){
        //可提供模糊查找 ,查询只差留言，不查子留言
        List<MessageDTO> pageInfo = messageService.selectMessageByCondition(req.getContent());
        return R.ok("查询成功").put("pageInfo",pageInfo);
    }

    @PostMapping("/personalMessageCenter")
    @ApiOperation("查看个人留言中心")
    public R personalMessageCenter(HttpServletRequest httpServletRequest){
        Integer userId = getUserId(httpServletRequest);
        List<MessageDTO> pageInfo = messageService.personalMessageCenter(userId);
        return R.ok("查询成功").put("pageInfo",pageInfo);
    }
    //仿淘宝进入管理的页面,messageDTO中的userId跟用户Id相匹配就能就行删除
    @PostMapping("/personalMessageCenter/delete")
    @ApiOperation("删除个人留言中心中某条评论")
    public R personalMessageCenterDelete(@Valid @RequestBody UniAppDeleteMessageReq req,
                                         HttpServletRequest httpServletRequest){
        Integer userId = getUserId(httpServletRequest);
        messageService.personalMessageCenterDelete(req.getMessageId(),req.getSonMessageId(),userId,userId);
        return R.ok("删除成功");
    }

    private Integer getUserId(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String id = claims.getSubject();
        int userId = Integer.parseInt(id);
        return userId;
    }
}
